package com.wanjia.estate.service.httpservice;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.wanjia.estate.pojo.bdc.*;
import com.wanjia.estate.pojo.bdc.getycslbh.GetycslbhResponse;

import com.wanjia.estate.pojo.bdc.registrat.RegistratRequest;
import com.wanjia.estate.pojo.bdc.registrat.RegistratResponse;
import com.wanjia.estate.pojo.bdc.spfhtcx.SpwqRequest;
import com.wanjia.estate.pojo.bdc.spfhtcx.SpwqResponse;
import com.wanjia.estate.pojo.bdc.suit.TcRequest;
import com.wanjia.estate.pojo.bdc.suit.TcResponse;
import com.wanjia.estate.pojo.property.PropertyRequest;
import com.wanjia.estate.pojo.property.PropertyResponse;

import com.wanjia.estate.properties.HttpRequestProperties;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PropertyHttpService extends AbstractHttpService {

    @Setter(onMethod_ = @Autowired)
    private HttpRequestProperties properties;

    /**
     * 查房
     * @param request
     * @return
     */
    public PropertyResponse execute(PropertyRequest request) {
        RequestEntity<String> requestEntity = RequestEntity.post(properties.getPropertyUrl())
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSONUtil.toJsonStr(request));
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity, PropertyResponse.class);
    }

    /**
     * 合同查询
     * @param spwqRequest
     * @return
     */
    public SpwqResponse Spfhtsjcx(SpwqRequest spwqRequest){
        String contractUrl = properties.getContractUrl();
        RequestEntity<SpwqRequest> requestEntity = RequestEntity.post(contractUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(spwqRequest);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity, SpwqResponse.class);
    }

    /**
     * 套次查询
     * @param tcRequest
     * @return
     */
    public TcResponse Cqrfwtccx(TcRequest tcRequest){
        String suitUrl = properties.getSuitUrl();
        RequestEntity<TcRequest> requestEntity = RequestEntity.post(suitUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(tcRequest);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity, TcResponse.class);
    }


    /**
     * 产权登记信息查询(首次登记证明查询)
     * @param registratRequest
     * @return
     */
    public RegistratResponse Registrat(RegistratRequest registratRequest){
        String registratUrl = properties.getRegistratUrl();
        RequestEntity<RegistratRequest> requestEntity = RequestEntity.post(registratUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(registratRequest);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity,RegistratResponse.class);
    }

    /**
     * 获取一窗受理编号
     * @return
     */
    public GetycslbhResponse getycslbh(){
        String getycslbhUrl = properties.getGetycslbhUrl();
        RequestEntity<Object> requestEntity = RequestEntity.post(getycslbhUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(HttpEntity.EMPTY);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity, GetycslbhResponse.class);
    }

    /**
     * 推送业务数据给一窗
     * @return
     */
    public Ywsl.Response pushYcsl(Ywsl.Request request){
        String setywxxUrl = properties.getSetywxxUrl();
        RequestEntity<String> requestEntity = RequestEntity.post(setywxxUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSON.toJSONString(request));
        log.info("请求request：{}",requestEntity);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity, Ywsl.Response.class);
    }


    /**
     * 接收外部系统的电子材料数据
     */
    public SetDzcl.Response setDzcl(SetDzcl.Request request){
        String setdzclUrl = properties.getRemote().getSetdzclUrl();
        RequestEntity<String> requestEntity = RequestEntity.post(setdzclUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(JSON.toJSONString(request));
        log.info("请求request：{}",requestEntity);
//        ResponseEntity<SetDzcl.Response> entity = template.exchange(requestEntity, SetDzcl.Response.class);
//        return getBody(entity);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity,SetDzcl.Response.class);
    }

    /**
     * 修改缴费状态
     */
    public UpdateJfzt.Response updateJfzt(UpdateJfzt.JfztRequest request){
        String updatejfztUrl = properties.getRemote().getUpdatejfztUrl();
        RequestEntity<UpdateJfzt.JfztRequest> requestEntity = RequestEntity.post(updatejfztUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(request);
//        ResponseEntity<UpdateJfzt.Response> entity = template.exchange(requestEntity, UpdateJfzt.Response.class);
//        return getBody(entity);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        return parseObjResponseEntity(entity, UpdateJfzt.Response.class);
    }


    /**
     * 撤件
     */
    public UpdateYwzt.Response updateYwzt(UpdateYwzt.Request request){
        String updatejfztUrl = properties.getRemote().getUpdatejfztUrl();
        RequestEntity<UpdateYwzt.Request> requestEntity = RequestEntity.post(updatejfztUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(request);
        ResponseEntity<UpdateYwzt.Response> entity = template.exchange(requestEntity, UpdateYwzt.Response.class);
        log.info("响应response：{}",entity);
        return getBody(entity);
    }


    /**
     * 删件
     */
    public DeleteYwxx.Response deleteYwxx(DeleteYwxx.Request request){
        String deleteywxxUrl = properties.getRemote().getDeleteywxxUrl();
        RequestEntity< DeleteYwxx.Request> requestEntity = RequestEntity.post(deleteywxxUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(request);
        ResponseEntity< DeleteYwxx.Response> entity = template.exchange(requestEntity, DeleteYwxx.Response.class);
        return getBody(entity);
    }
}
